package com.service.healthycode.rabbitmq;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.rabbitmq.client.Channel;
import com.service.config.utils.CharUtil;
import com.service.config.utils.IdUtil;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.healthycode.config.RabbitConfig;
import com.service.healthycode.dao.UserDao;
import com.service.healthycode.dto.SmsCode;
import com.service.healthycode.entity.UserInfo;
import com.service.healthycode.task.SendSmsOne;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @ClassName: MsgReceiver
 * @Description:
 * @Author guochuang
 * @Date 2021/10/25
 * @Version 1.0
 */
@Component
public class MsgReceiver {
    private final ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
    private final ExecutorService product = new ThreadPoolExecutor(10,20,200L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>(),namedThreadFactory);
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserDao userInfoDao;
    @Resource
    private SendSmsOne sendSmsOne;

    //登录验证码
    @RabbitListener(queues = RabbitConfig.QUEUE_A)
    public void processC(Channel channel, String content, Message message) throws Exception {
        logger.info(content);
        logger.info(">>>>>>>>>>>>>>>>>message："+JsonXMLUtils.json2obj(content, SmsCode.class));
        SmsCode smsCode = JsonXMLUtils.json2obj(content, SmsCode.class);
        try {
            UserInfo userInfo = userInfoDao.queryByPhone(smsCode.getTelephone());
            redisUtil.set(smsCode.getCode(),1,JsonXMLUtils.obj2json(userInfo),3);
           // sendSmsOne.sendSms(smsCode,1);
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>error"+e.getMessage());
        }
    }
}
